local skel = fk.CreateSkill{
  name = "ksp__jiran",
}

Fk:loadTranslationTable{
  ["ksp__jiran"] = "戟染",
  [":ksp__jiran"] = "你可以将N张牌当牌名字数为X+N或X-N的伤害牌使用，若N为0，此技能本回合失效。（X为自你上次造成伤害后，你使用的牌数，初始为0）",
  ["#ksp__jiran"] = "戟染：你可以将N张牌当牌名字数为 %arg+N 或 %arg-N 的伤害牌使用",
  ["@[ksp__jiran]"] = "戟染",

  ["$ksp__jiran1"] = "骄狂纵意，天地唯我！",
  ["$ksp__jiran2"] = "罡风贯耳，为我战歌！",
}

Fk:addQmlMark{
  name = "ksp__jiran",
  qml_path = "",
  how_to_show = function(name, value, p)
    local number = tonumber(value)
    if number then
      return tostring(number - 1)
    end
    return "??"
  end,
}

skel:addEffect("viewas", {
  prompt = function (self, player, selected_cards, selected)
    local x = player:getMark("@[ksp__jiran]") - 1
    return "#ksp__jiran:::"..x
  end,
  pattern = ".|0|red,black,nosuit",
  interaction = function(self, player)
    local all_names = table.filter(Fk:getAllCardNames("bt"),function (name) return Fk:cloneCard(name).is_damage_card end)
    local names = player:getViewAsCardNames(skel.name, all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return table.contains(player:getHandlyIds(), to_select)
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    local x = player:getMark("@[ksp__jiran]") - 1
    local card = Fk:cloneCard(self.interaction.data)
    local n = Fk:translate(card.trueName, "zh_CN"):len()
    if not (n == x + #cards or n == x- #cards) then return end
    card:addSubcards(cards)
    card.skillName = skel.name
    return card
  end,
  before_use = function(self, player, use)
    if #use.card.subcards == 0 then
      player.room:invalidateSkill(player, skel.name, "-turn")
    end
    local n = Fk:translate(use.card.trueName, "zh_CN"):len()
    for _, id in ipairs(use.card.subcards) do
      n = n - Fk:translate(Fk:getCardById(id).trueName, "zh_CN"):len()
    end
    if n == 0 then
      use.extra_data = use.extra_data or {}
      use.extra_data.chengqi_draw = player
    end
  end,
  enabled_at_response = function(self, player, response)
    return not response and #player:getHandlyIds() > 1 and
      #player:getViewAsCardNames(skel.name, Fk:getAllCardNames("bt"), nil, player:getTableMark("chengqi-turn")) > 0
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player,"@[ksp__jiran]",1)
end)
skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player,"@[ksp__jiran]",0)
end)

skel:addEffect(fk.Damage,{
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name,true) and target == player
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"@[ksp__jiran]",1)
  end,
})

skel:addEffect(fk.CardUsing,{
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name,true) and target == player
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player,"@[ksp__jiran]",1)
  end,
})

return skel